package com.cup.tianjin;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.internal.BottomNavigationItemView;
import android.support.design.internal.BottomNavigationMenuView;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.Toast;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    //声明ViewPager
    private ViewPager mViewPager;
    private FragmentPagerAdapter mAdapter;
    private List<Fragment> mFragment;

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_trend:
                    //    mTextMessage.setText(R.string.trend);
                    mViewPager.setCurrentItem(0);
                    return true;
                case R.id.navigation_grade:
                    //     mTextMessage.setText(R.string.grade);
                    mViewPager.setCurrentItem(1);
                    return true;
                case R.id.navigation_category:
                    //    mTextMessage.setText(R.string.category);
                    mViewPager.setCurrentItem(2);
                    return true;
                case R.id.navigation_zone:
                    //     mTextMessage.setText(R.string.zone);
                    mViewPager.setCurrentItem(3);
                    return true;
                case R.id.navigation_department:
                    //    mTextMessage.setText(R.string.department);
                    mViewPager.setCurrentItem(4);
                    return true;
            }
            return false;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mViewPager = (ViewPager) findViewById(R.id.viewpager);
        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        //调用自定义函数，控制ButtonNavigationView的切换动画效果
        disableShiftMode(navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
        initDatas();


        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayShowTitleEnabled(false);//去掉title的label
        actionBar.setDisplayShowHomeEnabled(false);//隐藏title的logo和icon
        /** An array of strings to populate dropdown list */
        final String[] actions = getResources().getStringArray(R.array.login_title);
        /** Create an array adapter to populate dropdownlist */
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, actions);
        /** Enabling dropdown list navigation for the action bar */
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
        /** Defining Navigation listener */
        ActionBar.OnNavigationListener navigationListener = new ActionBar.OnNavigationListener() {
            @Override
            public boolean onNavigationItemSelected(int itemPosition, long itemId) {
                Toast.makeText(MainActivity.this, "You selected : " + actions[itemPosition], Toast.LENGTH_SHORT).show();
                return false;
            }
        };
        /**
         * Setting dropdown items and item navigation listener for the actionbar
         */
        actionBar.setListNavigationCallbacks(adapter, navigationListener);

    }

    private void initDatas() {
        mFragment = new ArrayList<>();
        //将五个Fragment加入集合中
        mFragment.add(new TrendFragment());
        mFragment.add(new GradeFragment());
        mFragment.add(new CategoryFragment());
        mFragment.add(new ZoneFragment());
        mFragment.add(new DepartmentFragment());
        //初始化适配器
        mAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public android.support.v4.app.Fragment getItem(int position) {
                return mFragment.get(position);
            }

            @Override
            public int getCount() {
                return mFragment.size();
            }
        };
        //设置ViewPager的适配器
        mViewPager.setAdapter(mAdapter);
        //设置ViewPager的切换监听
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            //页面滚动事件
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                //设置position对应的集合中的Fragment
                //  mViewPager.setCurrentItem(position);
            }

            //页面选中事件
            @Override
            public void onPageSelected(int position) {
                //设置position对应的集合中的Fragment
                mViewPager.setCurrentItem(position);


            }

            //页面滚动状态改变事件
            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }


    //反射机制去掉ButtonNavigationView的切换动画效果
    @SuppressLint("RestrictedApi")
    public static void disableShiftMode(BottomNavigationView view) {
        BottomNavigationMenuView menuView = (BottomNavigationMenuView) view.getChildAt(0);
        try {
            Field shiftingMode = menuView.getClass().getDeclaredField("mShiftingMode");
            shiftingMode.setAccessible(true);
            shiftingMode.setBoolean(menuView, false);
            shiftingMode.setAccessible(false);
            for (int i = 0; i < menuView.getChildCount(); i++) {
                BottomNavigationItemView item = (BottomNavigationItemView) menuView.getChildAt(i);
                item.setShiftingMode(false);
                item.setChecked(item.getItemData().isChecked());
            }
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}
